package com.guyu.common.core.exception;

import com.guyu.common.core.entity.BizResult;
import com.guyu.common.core.exception.code.IErrorCode;
import lombok.Getter;


/**
 * 业务异常
 */
@Getter
public class BizException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    /**
     * 返回码
     */
    protected final Integer code;

    /**
     * 返回码描述
     */
    protected final String msg;

    /**
     * 业务码
     */
    protected final String bizCode;

    protected BizException(Integer code, String msg) {
        this(code, msg, "");
    }

    protected BizException(Integer code, String msg, Throwable cause) {
        this(code, msg, "", cause);
    }

    protected BizException(Integer code, String msg, String bizCode) {
        super(msg);
        this.code = code;
        this.msg = msg;
        this.bizCode = bizCode;
    }

    protected BizException(Integer code, String msg, String bizCode, Throwable cause) {
        super(msg, cause);
        this.code = code;
        this.msg = msg;
        this.bizCode = bizCode;
    }

    public static BizException create(IErrorCode error) {
        return new BizException(error.getCode(), error.getMsg());
    }

    public static BizException create(IErrorCode error, String msg) {
        return new BizException(error.getCode(), msg);
    }

    public static BizException create(IErrorCode error, Throwable cause) {
        return new BizException(error.getCode(), error.getMsg(), cause);
    }

    public static BizException create(IErrorCode error, String msg, Throwable cause) {
        return new BizException(error.getCode(), msg, cause);
    }

    /**
     * @param bizResult 错误结果集
     */
    public static BizException create(BizResult<?> bizResult) {
        return new BizException(bizResult.getCode(), bizResult.getMsg(), bizResult.getBizCode());
    }
}
